<?php

/**
 * 会话类
 * 
 * @package    01CMS
 * @subpackage system
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class Session
{

    /**
     * 写入Cookie
     * 
     * @param 	string $key
     * @param 	string $value
     * @param 	int $keepTime 	有效期（秒）
     * @param 	string $path
     * @return 	bool
     * @access 	public
     */
    public static function putCookie ($key, $value, $keepTime = 300, $path = "/")
    {
        $status1 = setcookie($key, $value, time() + $keepTime, $path);
        $status2 = setcookie($key . '_c', encrypt($value), time() + $keepTime, $path);
        return ($status1 && $status2);
    }

    /**
     * 删除COOKIE
     * 
     * @param 	string $key
     * @return 	bool
     * @access 	public
     */
    public static function dropCookie ($key)
    {
        $status1 = setcookie($key, '', time() - 360000, "/");
        $status2 = setcookie($key . '_c', '', time() - 360000, "/");
        return ($status1 && $status2);
    }

    /**
     * 获取Cookie值
     * 
     * @param 	string $key
     * @return 	mix
     * @access 	public
     */
    public static function getCookie ($key)
    {
        if (! isset($_COOKIE[$key]) || ! isset($_COOKIE[$key . '_c']))
        {
            return;
        }
        
        if ($_COOKIE[$key . '_c'] != encrypt($_COOKIE[$key]))
        {
            return;
        }
        
        return $_COOKIE[$key];
    }

    /**
     * 写入加密Cookie（设置验证码）
     * 
     * @param 	string 	$key
     * @param 	string 	$value
     * @param 	int 	$keepTime 有效期（秒）
     * @param 	string 	$path
     * @return 	bool
     * @access 	public
     */
    public static function setCode ($key, $value, $keepTime = 300, $path = "/")
    {
        return setcookie($key, encrypt(strtolower($value)), time() + $keepTime, $path);
    }

    /**
     * 检测Cookie值（检测验证码）
     * 
     * @param 	string 	$key
     * @param 	string 	$value
     * @return 	bool
     * @access 	public
     */
    public static function checkCode ($key, $value)
    {
        if (! isset($_COOKIE[$key]))
        {
            return FALSE;
        }
        
        $getedValue = $_COOKIE[$key];
        setcookie($key, '', time() - 360000, "/");
        return (encrypt(strtolower($value)) == $getedValue) ? true : false;
    }

    /**
     * 验证码
     */
    public static function validateCode ()
    {
        //create captcha
        $consts = 'cdfgkmnpqrstwxyz23456';
        $vowels = 'aek23456789';
        for ($x = 0; $x < 6; $x ++)
        {
            $const[$x] = substr($consts, mt_rand(0, strlen($consts) - 1), 1);
            $vow[$x] = substr($vowels, mt_rand(0, strlen($vowels) - 1), 1);
        }
        $radomstring = $const[0] . $vow[0] . $const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
        $string = substr($radomstring, 0, 4); //only display 4 str
        Session::setCode('validateCode', $string, 900);
        //set up image, the first number is the width and the second is the height
        $imageX = strlen($radomstring) * 8; //the image width
        $imageY = 20; //the image height
        $im = imagecreatetruecolor($imageX, $imageY);
        
        //creates two variables to store color
        $foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)), imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)), imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)), imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)));
        $middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
        $middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140), 80);
        
        //fill image with bgcolor
        imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
        //writes string
        if (function_exists('imagettftext'))
        {
            imagettftext($im, 18, rand(30, - 30), 5, rand(14, 16), $foregroundArr[rand(0, 3)], SYS_PATH . '/data/font/vrinda.ttf', $string[0]);
            imagettftext($im, 12, rand(50, - 50), 20, rand(14, 16), $foregroundArr[rand(0, 3)], SYS_PATH . '/data/font/ARIALNI.TTF', $string[1]);
            imagettftext($im, 12, rand(50, - 50), 35, rand(14, 16), $foregroundArr[rand(0, 3)], SYS_PATH . '/data/font/ALGER.TTF', $string[2]);
            imagettftext($im, 12, rand(30, - 30), 50, rand(14, 16), $foregroundArr[rand(0, 3)], SYS_PATH . '/data/font/arial.ttf', $string[3]);
        }
        else
        {
            imagestring($im, 5, 3, floor(rand(0, 5)) - 1, $string[0], $foregroundArr[rand(0, 3)]);
            imagestring($im, 5, 16, floor(rand(0, 5)) - 1, $string[1], $foregroundArr[rand(0, 3)]);
            imagestring($im, 5, 23, floor(rand(0, 5)) - 1, $string[2], $foregroundArr[rand(0, 3)]);
            imagestring($im, 5, 33, floor(rand(0, 5)) - 1, $string[3], $foregroundArr[rand(0, 3)]);
        }
        //strikethrough
        

        $border = imagecolorallocate($im, 200, 200, 200);
        //imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
        imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
        
        $pointcol = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
        for ($i = 0; $i < 80; $i ++)
        {
            imagesetpixel($im, rand(2, $imageX - 2), rand(2, $imageX - 2), $pointcol);
        }
        //random shapes
        for ($x = 0; $x < 9; $x ++)
        {
            if (mt_rand(0, $x) % 2 == 0)
            {
                imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
                imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
            }
            else
            {
                imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
                imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
            }
        }
        //output to browser
        header("content-type:image/png\r\n");
        imagepng($im);
        imagedestroy($im);
    
    }
}